package com.example.demo.listener;

import com.example.demo.bean.User;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class HelloReceiver {

    //一个生产者，一个消费者
    @RabbitListener(queues = "hello")
    @RabbitHandler
    public void hello(Message message, Channel channel) throws IOException {
//        System.out.println("Receiver:" + hello);
        try {
            //            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);中的false，是将自动确认机制关闭
            //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("接受到的消息为" + new String(message.getBody(), "UTF-8"));
            System.out.println(message.getMessageProperties().getCorrelationId());
        } catch (Exception e) {
//            如果我们把channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);中最后一个参数改为false呢，会发现在web管理界面没有未被消费的消息，说明这条消息已经被摒弃 trued的话会返回队列。
//            实际开发中，到底是打回到队列呢还是摒弃，要看自己的需求，但是打回队列应该有次数限制，不然会陷入死循环。
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }

    @RabbitListener(queues = "helloObject")
    @RabbitHandler
    public void hello2(Message message, Channel channel, User user) {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println(message.getMessageProperties().getDeliveryTag());
            System.out.println("接受到的消息为" + user.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //一个生产者，多个消费者，消费者均匀分配消息队列
    @RabbitListener(queues = "neo")
    @RabbitHandler
    public void neo1(Message message, Channel channel) {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("Receiver 1:" + new String(message.getBody(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "neo")
    @RabbitHandler
    public void neo2(Message message, Channel channel) {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("Receiver 1:" + new String(message.getBody(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发布订阅模式
    @RabbitListener(queues = "fanout.A")
    @RabbitHandler
    public void process1(String message) {
        System.out.println("fanout Receiver A:" + message);
    }

    @RabbitListener(queues = "fanout.B")
    @RabbitHandler
    public void process2(String message) {
        System.out.println("fanout Receiver B:" + message);
    }

    @RabbitListener(queues = "fanout.C")
    @RabbitHandler
    public void process3(String message) {
        System.out.println("fanout Receiver C:" + message);
    }

    /**
     * 主题模式
     */

    @RabbitListener(queues = "topic.A")
    @RabbitHandler
    public void topic1(String message) {
        System.out.println("Topic Receiver1  : " + message);
    }


    @RabbitListener(queues = "topic.B")
    @RabbitHandler
    public void topic2(String message) {
        System.out.println("Topic Receiver2  : " + message);
    }
}
